from typing import List

from loguru import logger
from fastapi import APIRouter
from starlette.responses import JSONResponse

from common.schema.taobao import *

from tasks.taobao import interface

tags = 'taobao'
taobao_router = APIRouter(
    prefix="/{}".format(tags),
    tags=[tags],
    # dependencies=[Depends(get_admin_token)],
    responses={404: {"description": "Not found"}},
    # on_startup=[get_adam_obj]
)


@taobao_router.post("/tb_goods", summary="创建淘宝买东西的任务", response_model=TaoBaoOrderTaskResponse)
def create_task(order_task: TaoBaoOrderTask):
    try:
        result = interface.create_phone_task_thread(order_task)
        return result
    except Exception as e:
        result = order_task.dict()
        result['err'] = str(e)
        logger.error("android_ip={}, func={}, err={}, value={}".format(
            order_task.android_ip, 'create_task', str(e), result))
        return JSONResponse(status_code=400, content=result)


@taobao_router.get("/all_task_status", summary="获取数据库中所有任务的状态", response_model=List[TaoBaoOrderTaskResponse])
def all_task_status():
    try:
        result = interface.get_all_task_status()
        return result
    except Exception as e:
        result = {"err": str(e)}
        logger.error("func={}, err={}, value={}".format('task_status', str(e), result))
        return JSONResponse(status_code=400, content=result)
